package com.foyekoo.project001.command;

import android.content.Intent;
import android.provider.ContactsContract;
import android.util.Log;

import com.foyekoo.project001.App;
import com.foyekoo.project001.config.DataConfig;
import com.foyekoo.project001.service.UsbService;
import com.foyekoo.project001.utils.TimeUtil;

import java.util.ArrayList;

public final class Command {


    /**
     * 获取游戏数量
     */
    public static void getGameCoinNumber(){

    }

    /**
     * io板子连接确认
     */
    public static void IOConnectionConfirm(){
        byte[] cur = DataConfig.IO_CONNECTION.clone();
        ArrayList<Integer> time = TimeUtil.formatTimeClick(TimeUtil.getTimeFormat());

        cur[4] = (byte) (time.get(1) +12);
        cur[5] = (byte)(time.get(2) + 31);
        cur[6] = (byte)(time.get(3) + 24);


        send(cur);
    }

    /**
     * 免费模式
     * @param isOpen
     */
    public static void freeMode(boolean isOpen){//main
        byte[] cur = DataConfig.FREE_MODE.clone();
        if(isOpen) {
            cur[3] = 0x01;
        } else {
            cur[3] = 0x00;
        }
        send(cur);
    }

    /**
     * 是否开启掉电保存
     * @param isOpen
     */
    public static void updateSaveStatus(boolean isOpen){
        byte[] cur = DataConfig.SAVE_STATUS.clone();//复制一份 不修改原本的 因为是个静态变量
        if (isOpen) {
            cur[4] = 0x01;
        } else {
            cur[4] = 0x00;
        }
        send(cur);
    }



    /**
     * 老化测试
     * @param isOpen
     */
    public static void oldTestControl(boolean isOpen){
        byte[] cur = DataConfig.OLD_CONTROL.clone();
        if(isOpen) {
            cur[4] = 0x01;
        } else{
            cur[4] = 0x00;
        }
        send(cur);
    }

    /**
     * 灯光测试控制
     * @param isOpen
     */
    public static void lightControl(boolean isOpen){
        byte[] cur = DataConfig.LIGHT_CONTROL.clone();
        if (isOpen) {
            cur[4] = 0x01;
        } else {
            cur[4] = 0x00;
        }
        send(cur);
    }



    /**
     * 电机最高点测试
     */
    public static void machineryMaxHeight(){
        byte[] data = DataConfig.MACHINERY_CONTROL_TEST.clone();
        data[3] = 0x02;
        data[4] = 0x03;
        send(data);
    }

    /**
     *
     * @param type
     * 1上 2 下 3 左 4右  5前 6 后
     */
    public static void machineryTest(int type , int cmd){
        byte[] data = DataConfig.MACHINERY_CONTROL_TEST.clone();
        data[3] = (byte) type;
        data[4] = (byte) cmd;

      /*  if (type == 1) { //上
            data[4] = 0x01;
            data[5] = 0x01;
        } else if (type == 2) { //下
            data[4] = 0x01;
            data[5] = 0x02;
        } else if (type == 3) { // 左
            data[4] = 0x00;
            data[5] = 0x01;
        } else if (type == 4) { //右
            data[4] = 0x00;
            data[5] = 0x02;
        } else if (type == 5) { //前
            data[4] = 0x02;
            data[5] = 0x01;
        } else if (type == 6) {//后
            data[4] = 0x02;
            data[5] = 0x02;
        }*/
        send(data);
    }


    public static void gameTimeSet(int time){
        byte[] cur = DataConfig.DATA_GAME_TIME_BY_ONE.clone();
        cur[4] = (byte) time;
        send(cur);
    }

    public static void gameCoinSet(int coin){
        byte[] cur = DataConfig.GAME_COIN_BY_ONE.clone();
        cur[4] = (byte) coin;
        send(cur);
    }

    public static void dataInit(){
        byte[] cur = DataConfig.DATA_INIT.clone();
        send(cur);
    }

    /**
     * 电机速度
     *
     * @param z
     * @param y
     */
    public static void machinerySpeed(int z , int y){
        byte[] cur = DataConfig.MACHINERY_SPEED.clone();
        cur[4] = (byte) z;
        cur[5] = (byte) y;
        send(cur);
    }

    /**
     * 游戏页面成功
     */
    public static void gameUILoadingSuccess(){
        byte[] cur = DataConfig.GAME_UI_SUCCESS;
        send(cur);
     }

    /**
     * 等待页面成功
     */
    public static void waitingUILoadingSuccess(){
        byte[] cur = DataConfig.WAITING_UI_SUCCESS;
        send(cur);
    }



    /**
     * 重置的时候 发给mcu
     */
    public static void setDefault(){
        new Thread(){
            public void run(){
                try {
                    Thread.sleep(30);


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 能力
     * @param z1
     * @param z2
     */
    public static void machineryPower(int z1 , int z2) {
        byte[] cur = DataConfig.MACHINERY_POWER.clone();
        cur[4] = (byte) z1;
        cur[5] = (byte) z2;
        send(cur);
    }

    /**
     * 等待时间
     * @param z1
     * @param z2
     */
    public static void  machineryWaiting(int z1 , int z2){
        byte[] cur = DataConfig.MACHINERY_WAITING.clone();
        cur[4] = (byte) z1;
        cur[5] = (byte) z2;
        send(cur);
    }

    /**
     * 强推
     * @param z1
     * @param z2
     */
    public static void machineryPush(int z1 , int z2){
        byte[] cur = DataConfig.MACHINERY_PUSH.clone();
        cur[4] = (byte) z1;
        cur[5] = (byte) z2;
        send(cur);
    }



    public static void send(byte[] data){
        Intent intent = new Intent(UsbService.ACTION_DATA_RECEIVE);
        intent.putExtra("data",data);
        App.getInstance().sendBroadcast(intent);
        Log.d("acthome","send");

    }
}
